import React from 'react';
import { Router, Route, Switch,Redirect } from 'dva/router';
import { LocaleProvider, Spin ,message} from 'antd';
import zhCN from 'antd/lib/locale-provider/zh_CN';
import dynamic from 'dva/dynamic';
import { getRouterData } from './common/router';

import styles from './index.less';
import docCookies from "./common/DocCookies";

dynamic.setDefaultLoadingComponent(() => {
  return <Spin size="large" className={styles.globalSpin} />;
});

/*function RouterConfig({ history, app }) {
 const routerData = getRouterData(app);
 const UserLayout = routerData['/user'].component;
 const BasicLayout = routerData['/'].component;
 return (
 <LocaleProvider locale={zhCN}>
 <Router history={history}>
 <Switch>
 <Route path="/user" render={props => <UserLayout {...props} />} />
 <Route path="/" render={props => <BasicLayout {...props} />} />
 </Switch>
 </Router>
 </LocaleProvider>
 );
 }*/

function RouterConfig({ history, app }) {
  const routerData = getRouterData(app);
  const UserLayout = routerData['/user'].component;
  const BasicLayout = routerData['/'].component;
  return (
    <LocaleProvider locale={zhCN}>
      <Router history={history}>
        <Switch>
          <Route path="/user" render={props => <UserLayout {...props} />} />
          <Route path="/" render={props => requireAuth(BasicLayout, props)} />
        </Switch>
      </Router>
    </LocaleProvider>
  );
}

// 登录验证
function requireAuth(Layout, props, passProps) {
  // 模拟token失效时间
  if(docCookies.getItem("token")){
    return <Layout {...props} {...passProps} />;
  }else{
    if(docCookies.getItem("expiresTime")){
      var expires = new Date();
      if(expires.getTime >= docCookies.getItem("expiresTime")) {
        clearInterval(global.auditMsg);
        message.warning("登录超时，请重新登录！");
      }
    }
    return <Redirect to="/user/login" />;
  }
}

export default RouterConfig;
